package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.ContractInvoice;
import net.hn.hnms.biz.project.domain.ContractInvoicePayment;
import net.hn.hnms.biz.project.domain.ContractPayment;
import net.hn.hnms.biz.project.mapper.ContractInvoicePaymentMapper;
import net.hn.hnms.biz.project.mapper.ContractPaymentMapper;
import net.hn.hnms.biz.project.request.ContractInfoReq;
import net.hn.hnms.biz.project.request.ContractPaymentReq;
import net.hn.hnms.biz.project.service.ContractPaymentService;
import net.hn.hnms.biz.project.vo.ContractInfoVo;
import net.hn.hnms.biz.project.vo.ContractPaymentVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ContractPaymentServiceImpl implements ContractPaymentService {

    @Autowired
    private ContractPaymentMapper contractPaymentMapper;

    @Autowired
    private ContractInvoicePaymentMapper contractInvoicePaymentMapper;

    @Override
    @Transactional
    public int insert(ContractPaymentReq contractPaymentReq) {
        ContractPayment insertObj = new ContractPayment();
        BeanUtil.copyProperties(contractPaymentReq, insertObj);
        int flag = contractPaymentMapper.insert(insertObj);
        String invoiceId = null;
        invoiceId = contractPaymentReq.getInvoiceId();
        if(invoiceId != null){
            ContractInvoicePayment pay = new ContractInvoicePayment();
            pay.setInvoiceId(invoiceId);
            pay.setPaymentId(insertObj.getContractPaymentId());
            contractInvoicePaymentMapper.insert(pay);
        }
        return flag;
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        contractPaymentMapper.updateDeleteByIds(idList);
    }

    @Override
    @Transactional
    public void updateById(ContractPaymentReq contractPaymentReq) {
        ContractPayment updateObj = new ContractPayment();
        BeanUtil.copyProperties(contractPaymentReq, updateObj);
        int updateCount = contractPaymentMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        String invoiceId = null;
        invoiceId = contractPaymentReq.getInvoiceId();
        if(invoiceId != null){
            ContractInvoicePayment pay = new ContractInvoicePayment();
            pay.setInvoiceId(invoiceId);
            pay.setPaymentId(contractPaymentReq.getContractPaymentId());
            contractInvoicePaymentMapper.updateContractInvoicePaymentByInvoice(pay);
        }

    }


    @Override
    public ContractPayment selectDetailById(String id) {
        ContractPayment contractPayment = contractPaymentMapper.selectById(id);
        if (contractPayment == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return contractPayment;
    }

    @Override
    public ResponsePage<ContractPaymentVo> selectPageList(ContractPaymentReq contractPaymentReq, PageQuery pageQuery) {
//        Page<ContractPayment> page = contractPaymentMapper.selectPage(pageQuery.build(), buildQuery(contractPaymentReq).orderByDesc(ContractPayment::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        Page<ContractPaymentVo> page = contractPaymentMapper.selectPage(pageQuery.build(),contractPaymentReq);
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<ContractPayment> buildQuery(ContractPaymentReq req){
        return new LambdaQueryWrapper<ContractPayment>()
                .like(StrUtil.isNotBlank(req.getPaymentCompany()), ContractPayment::getPaymentCompany, req.getPaymentCompany())
                .like(StrUtil.isNotBlank(req.getContractId()), ContractPayment::getContractId, req.getContractId());
    }

    @Override
    public ResponsePage<ContractInfoVo> selectContractPageList(ContractInfoReq contractInfoReq, PageQuery pageQuery) {
        Page<ContractInfoVo> page = contractPaymentMapper.selectPaymentPageList(pageQuery.build(), contractInfoReq);
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public List<ContractInvoice> getContractInvoiceList(ContractInfoReq contractInfoReq) {
        return contractPaymentMapper.getContractInvoiceList(contractInfoReq);
    }

}
